<?php
namespace app\home\model;

use think\Model;

class BaseModel extends Model
{
    public $page        =1;
    public $pageNum     =0;
    public $pageView    ='';
    public $pageCount   =0;
    public $dataMsg     ='';
    public $field       =[];
    public $isObject    =false;
    public $pageAll     =false;
    public $order       ='';

    public function __construct($data = [])
    {
        parent::__construct($data);
        if(!$this->pageNum){
            $list_rows  = config('paginate.list_rows');
            if($list_rows) $this->pageNum=!empty($list_rows)?$list_rows:15;
        }
    }

    /**模型实力化
     * @param string $table
     * @return Common|array|\think\db\Query
     */
    private function initModel($table=''){
        if($this->isObject === false){
            if(empty($table)){
                $name   = $this->name;
            }else{
                $name   = $table;
            }
            if(empty($name)){
                return [];
            }
            $model =db($name);
        }else{
            $model =$this;
        }
        return $model;
    }

    /**获取某个字段值
     * @param array $where
     * @param string $filds
     * @param string $table
     * @return mixed
     */
    public function getValue($where=[],$filds='',$table=''){
        $model  = self::initModel($table);
        $val    = $model->where($where)->value($filds);
        return $val;
    }

    /**获取一条数据
     * @param array $where
     * @param array $filds
     * @param string $table
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public  function getFind($where=[],$table='',$filds=[]){
        $model  = self::initModel($table);
        $val    = $model->where($where)->field($filds)->find();
        return $val;
    }
    /**获取数量
     * @param $model
     * @return int
     */
    private function pageCount($where=[],$table=''){
        $model=self::initModel($table);
        $this->pageCount=  $model->where($where)->count();
        return $this->pageCount;
    }
    /**
     * @param array $where
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getPageList($where=[],$bool=false,$table=''){
        $model=self::initModel($table);
        if(!empty($this->field)){
            $model=$model->field($this->field);
        }
            $model->where($where);
        if(!empty($this->order)){
            $model->order($this->order);
        }
        if($bool === true){
            $re            =$model->paginate($this->pageNum,false,['query'=>request()->get()]);
            $result        =$re->items();
            $this->pageView=$re->render();
        }else{
            if($this->pageAll === false){
                $model->page($this->page,$this->pageNum);
            }
            $result = $model->select();
        }
        return $result;
    }

    public function getColumnName($tabel=''){
        $table_str    = config('database.database');
        if(empty($tabel) || empty($table_str)){
            return [];
        }
        $fil = "select COLUMN_NAME from information_schema.COLUMNS where table_name = '".$tabel."' and table_schema = '".$table_str."';";
        $tab= Db::query($fil);
        if(empty($tab)){
            return [];
        }
        $rel=[];
        foreach($tab as $key=>$val){
            $rel[]=$val['COLUMN_NAME'];
        }
        return $rel;
    }
}